package gui.awt.listener;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;

public class TestMouseL {
    public static void main(String[] args) {
        new MyFrame01("画图");
    }
}

class MyFrame01 extends Frame{

    private ArrayList points;
    //需要画笔、监听鼠标当前位置、存储点
    public MyFrame01(String title){
        super(title);
        setBounds(200,200,400,300);

        //存鼠标点击的点
        this.points = new ArrayList();
        //鼠标监听器
        this.addMouseListener(new MyMouseListener());

        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        //画画，监听鼠标事件
        Iterator iterator = this.points.iterator();
        while (iterator.hasNext()){
            Point point = (Point) iterator.next();
            g.setColor(Color.cyan);

            g.fillOval(point.x,point.y,10,10);
        }
    }

    //添加一个点
    public void addPoint(Point point){
        this.points.add(point);
    }

    //鼠标监听器
    private class MyMouseListener extends MouseAdapter {

        @Override
        public void mouseClicked(MouseEvent e) {
            MyFrame01 frame = (MyFrame01) e.getSource();
            frame.addPoint(new Point(e.getX(),e.getY()));
//            System.out.println(e.getX() + "-" +e.getY());

            //点击重画，刷新
            frame.repaint();
        }
    }
}
